//Please run our Campus Map applet from the Model.java file.

package CampusMap.View;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import CampusMap.Controller.Controller;
import CampusMap.Model.POI; 
import CampusMap.Model.Point;
import CampusMap.Model.poiInOut;





public class View extends JPanel implements Observer, ActionListener {
		
	private Controller control;
	private Point pnt;
	private ArrayList<Point> digList = new ArrayList<Point>();
	private ArrayList<POI> poiList = new ArrayList<POI>();
	protected int userX, userY;
	private static BufferedImage campus;
	private int mouseX, mouseY;
	public String modeStatus = "off";
	private poiInOut op1 = new poiInOut(poiList);
	private int curX=-1, curY=-1, poiIndex;
	private boolean inDrag = false;
	private int cursorStatus;
	//Create a file chooser
	final JFileChooser fc = new JFileChooser();
	private MouseListener mouseRelease;
	

	
	public View() {
		super(true);
		setPreferredSize(new Dimension(699,446));
		//createGUI();  taking care of this in main method right now...
	}

	public void createGUI(){
		
	
		
		JMenuBar menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("File");
		JMenu editMenu = new JMenu("Edit");
		
		JMenuItem LoadMenItem = new JMenuItem("Load POIs");
		JMenuItem LoadWebMenItem = new JMenuItem("Load Web POIs");
		JMenuItem SaveMenItem = new JMenuItem("Save POIs");			
		JMenuItem poiMenItem = new JMenuItem("Create/Move POIs");
		JMenuItem digMenItem = new JMenuItem("Digitize Path");
		
		//add menu to menubar
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		
		//add items to menus
		fileMenu.add(LoadMenItem);
		fileMenu.add(LoadWebMenItem);			
		fileMenu.add(SaveMenItem);
		editMenu.add(poiMenItem);
		editMenu.add(digMenItem);
		
	
		
		//adding action listeners for menu items
		
		digMenItem.addActionListener(this);
		LoadMenItem.addActionListener(this);
		LoadWebMenItem.addActionListener(this);
		poiMenItem.addActionListener(this);
		SaveMenItem.addActionListener(this);
	
	
		
		//action listeners for mouse stuff
		//control.mouseReleased(arg0)
		//gui.addMouseListener(gui);
		
		//Create JFrame to place other components such as panels or buttons within.
		JFrame frame = new JFrame("Campus Map: Points of Interest and Digitize Path");

		//Add an instance of the MinimalDrawingGUI to the frame.
		frame.add(this);
		
		//Set the size, arrange components, and display the frame.
		frame.pack();
		frame.setVisible(true);
		
		//Exit on close.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//set menu in frame
		frame.setJMenuBar(menuBar);
		
		
	}

	public void paintComponent(Graphics g) {
        super.paintComponent(g);    // Paints all other stuff, e..g., background. (Is this defined in JPanel?).
       
        // Cast to a Graphics2D object to support affine transformations.
        Graphics2D g2d = (Graphics2D) g;
        
        //Draw the background campus map.
      //Load the image (this time we do not handle the exception but throw it somewhere else ;-))
      		try {
				campus = ImageIO.read(new File("C:\\java\\campus.png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        g2d.drawImage(campus, 0, 0, this);	   
       
       	        	        
        //Draw Digitized Path.
        if (digList != null){
        	
        	//Draw Digitized Path Points.
        	for (int i=0; i<digList.size(); i++){
        		g2d.setColor(Color.blue);
        		g2d.fillOval(
        				(int) digList.get(i).getX()-4,
        				(int) digList.get(i).getY()-4,
        				(int) 2*4,
        				(int) 2*4
        				);
	        }
        	
        	//Draw Digitized Path lines if more than one Point.
        	if (digList.size() > 1){
        		for (int i=0; i < digList.size()-1; i++){
        			g2d.drawLine(
        					(int) digList.get(i).getX(),
        					(int) digList.get(i).getY(),
        					(int) digList.get(i+1).getX(),
        					(int) digList.get(i+1).getY()
        					);
        		}
        	}
        }
	       	        
        //Draw POIs. 
        if (poiList != null){
        	
        	//Draw POI Points.
        	for (int i=0; i<poiList.size(); i++){
        		g2d.setColor(Color.red);
        		g2d.fillOval(
        				(int) poiList.get(i).getPt().getX()-4,
        				(int) poiList.get(i).getPt().getY()-4,
        				(int) 2*4,
        				(int) 2*4
        				);
        	}
        }
     
        //Draw POI Labels, if the Digitized Path Points are inside the POI PointBuffer.
        for (int dig = 0; dig < digList.size(); dig++){
        	for (int poi = 0; poi < poiList.size(); poi++){
        		if (poiList.get(poi).isInside(digList.get(dig))) {
        			//Create font.
        			Font fontBold = new Font ("Helvetica", Font.BOLD, 16);
        			//Set font.
        			g2d.setFont(fontBold);
        			//Draw Label.
        			g2d.drawString(
        					poiList.get(poi).getLabel().toString(),
        					(int) poiList.get(poi).getPt().getX() + 6,
        					(int) poiList.get(poi).getPt().getY() + 6
        					);
        		}
        	}
        }

        
	} //End PAINT
	
	public static void main(String[] args) throws Exception{
		View gui = new View();
		gui.createGUI();
		//add instance of controller
		Controller control = new Controller(gui);
		control.addObserver(gui);
		
		
		
		
			
		
	}
	@Override
	public void update(Observable arg0, Object arg1) {
	
	if (modeStatus == "dig"){
		digList = (ArrayList<Point>) arg1;
	}
	if (modeStatus == "poi"){
		poiList = (ArrayList<POI>) arg1;
	}	
	
		repaint();
		
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		//Actions for clicking "Digitize Path" button.
		if (arg0.getActionCommand() == "Digitize Path"){

			//Print to console.
			System.out.println("");
			System.out.println("Switching modes ...");
			System.out.println("from: " + modeStatus);

			//Set status to "dig" (Digitize Path),
			//for use with overridden mouseRelease method.
			modeStatus = "dig";
			if (digList.size() > 0){
				ArrayList<Point> digList = new ArrayList<Point>();
				this.digList = digList;
			}			
			
			//Print to console.
			System.out.println("to: " + modeStatus);
							
			//Notify observers of change
			repaint();
		}

		
		//Actions for clicking "Enter Points of Interest" button.
		if (arg0.getActionCommand() == "Create/Move POIs"){
			
			//Print to console.
			System.out.println("");
			System.out.println("Switching modes ...");
			System.out.println("from: " + modeStatus);
			
			//Set status to "poi" (Enter Points of Interest),
			//for use with overridden mouseRelease method.
			modeStatus = "poi";

			//Print to console.
			System.out.println("to: " + modeStatus);
	
			//Repaint the GUI.
			repaint();
			//Notify observers of change
			
		}
		if (arg0.getActionCommand() == "Load POIs"){

			//Show Open dialog
			int returnVal = fc.showOpenDialog(this);

			//Read array list of POIs from user-specified binary file.
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
	            poiList = op1.readFile(file);// This may need altering
	            
				System.out.println("");
				System.out.println("POIs loaded from data file:");
				System.out.println(file);

	        }
			//Repaint the GUI.
			repaint();
		}
		
		if (arg0.getActionCommand() == "Load Web POIs"){

			    poiList = op1.readFileWeb();
				System.out.println("");
				System.out.println("POIs loaded from the internets");
				
	        }
			//Repaint the GUI.
			repaint();
		//}
		
		if (arg0.getActionCommand() == "Save POIs"){
			
			//Show Save dialog
			int returnVal = fc.showSaveDialog(this);
			
			//Write array list of POIs to user-specified binary file.
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fc.getSelectedFile();
				op1.writeFile(poiList, file);// This may need altering
				System.out.println("");
				System.out.println("POIs saved to data file:");
				System.out.println(file);
	        }
			//Repaint the GUI.
			repaint();
	}

}
}